package net.hn.hnms.biz.plan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.plan.domain.DimDrivageFace;
import net.hn.hnms.biz.plan.mapper.DimDrivageFaceMapper;
import net.hn.hnms.biz.plan.request.DimDrivageFaceReq;
import net.hn.hnms.biz.plan.service.IDimDrivageFaceService;
import net.hn.hnms.biz.plan.vo.DimDrivageFaceVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhaoyang
 * 掘进工作面管理服务层实现
 * @date 2022-08-01
 */

@Service
@DS("slave")
public class DimDrivageFaceServiceImpl  extends BaseService<DimDrivageFace, DimDrivageFaceVO> implements IDimDrivageFaceService {

    @Autowired
    private DimDrivageFaceMapper dimDrivageFaceMapper;

    /**
     * @param dimDrivageFace
     * @param pageQuery       查询掘进工作面列表
     */
    @Override
    public ResponsePage<DimDrivageFaceVO> selectPageList(DimDrivageFaceReq dimDrivageFace, PageQuery pageQuery) {

        Page<DimDrivageFace> page = dimDrivageFaceMapper.selectPage(pageQuery.build(), buildQuery(dimDrivageFace).orderByDesc(DimDrivageFace::getCreateTime));
        List<DimDrivageFaceVO> list = page.getRecords().stream().map(o -> convert(o, DimDrivageFaceVO.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DimDrivageFace> 查询条件
     */
    private LambdaQueryWrapper<DimDrivageFace> buildQuery(DimDrivageFaceReq req){
        return new LambdaQueryWrapper<DimDrivageFace>()
                .like(StrUtil.isNotBlank(req.getDrivageTunnelCode()), DimDrivageFace::getDrivageTunnelCode, req.getDrivageTunnelCode())
                .like(StrUtil.isNotBlank(req.getDrivageFaceCode()), DimDrivageFace::getDrivageFaceCode, req.getDrivageFaceCode())
                .like(StrUtil.isNotBlank(req.getDrivageFaceName()), DimDrivageFace::getDrivageFaceName, req.getDrivageFaceName())
                .ge(req.getBeginDateTime() != null, DimDrivageFace::getCreateTime, req.getBeginDateTime())
                .eq(StrUtil.isNotBlank(req.getIsKeyArea()), DimDrivageFace::getIsKeyArea, req.getIsKeyArea())

                .le(req.getEndDateTime() != null, DimDrivageFace::getCreateTime, req.getEndDateTime())
//                .eq(StrUtil.isNotBlank(req.getMineCode()), DimDrivageFace::getMineCode, req.getMineCode())
                .eq(StrUtil.isNotBlank(req.getStatus()), DimDrivageFace::getStatus, req.getStatus())
                .and(o->o.eq(req.getMineCode()!=null,DimDrivageFace::getMineCode, req.getMineCode()).or().eq(req.getCompanyCode()!=null, DimDrivageFace::getCompanyCode, req.getCompanyCode()));
    }

    /**
     *
     * @param dimDrivageFace
     * 查询掘进工作面列表
     */
    @Override
    public List<DimDrivageFaceVO> selectList(DimDrivageFaceReq dimDrivageFace) {
        List<DimDrivageFace> list = dimDrivageFaceMapper.selectList(buildQuery(dimDrivageFace));
        return list.stream().map(o -> convert(o, DimDrivageFaceVO.class)).collect(Collectors.toList());
    }

    /**
     * @param dimDrivageFace 新增掘进工作面
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DimDrivageFaceReq dimDrivageFace) {
        if (dimDrivageFace.getDrivageFaceEndDate().before(dimDrivageFace.getDrivageFaceStartDate())) {
            throw new HuaNengException(Constants.FAIL, "预计开工时间不能在预计竣工时间之后");
        }
        QueryWrapper<DimDrivageFace> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("drivage_face_code", dimDrivageFace.getDrivageFaceCode());

        if (dimDrivageFaceMapper.selectCount(queryWrapper) > 0) {
            throw new HuaNengException(Constants.FAIL, "已存在相同的工作面编号");
        }
        DimDrivageFace insertObj = new DimDrivageFace();
        BeanUtil.copyProperties(dimDrivageFace, insertObj);
        insertObj.setStatus("0");
        return dimDrivageFaceMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除 id集合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        dimDrivageFaceMapper.deleteBatchIds(idList);
    }

    /**
     * @param dimDrivageFace 单个修改 按照主键修改信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(DimDrivageFaceReq dimDrivageFace) {
        if (dimDrivageFace.getDrivageFaceEndDate().before(dimDrivageFace.getDrivageFaceStartDate())) {
            throw new HuaNengException(Constants.FAIL, "预计开工时间不能在预计竣工时间之后");
        }
        if (StrUtil.isBlank(dimDrivageFace.getDrivageFaceId())) {
            throw new HuaNengException(Constants.FAIL, "id不能为空");
        }
        DimDrivageFace updateObj = new DimDrivageFace();
        BeanUtil.copyProperties(dimDrivageFace, updateObj);
        int updateCount = dimDrivageFaceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    /**
     * @param id 按主键id查询详情
     */
    @Override
    public DimDrivageFaceVO selectDetailById(String id) {
        DimDrivageFace dimDrivageFace = dimDrivageFaceMapper.selectById(id);
        if (dimDrivageFace == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return convertWithUser(dimDrivageFace, DimDrivageFaceVO.class);
    }

    /**
     * 启用/禁用
     * @param id 数据id
     * @param status  status: 0:启用 1:禁用
     */
    public void changeStatus(String id, Integer status) {
        if (status == 1 || status == 0) {
            DimDrivageFace planMining = dimDrivageFaceMapper.selectById(id);
            if (planMining == null) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            planMining.setStatus(status + "");
            int res = dimDrivageFaceMapper.updateById(planMining);
            if (res == 0) {
                throw new HuaNengException(Constants.FAIL, "数据不存在");
            }
            return;
        }
        throw new HuaNengException(Constants.FAIL, "传入的参数错误");
    }
}
